﻿
namespace MVVMApp_WFBasedBusinessRules.Converters
{
    using System;
    using System.Windows.Data;

    class ProductQuantityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, 
            object parameter, 
            System.Globalization.CultureInfo culture)
        {
            return value;
        }

        [System.Diagnostics.DebuggerStepThrough]
        public object ConvertBack(object value, Type targetType, 
            object parameter, 
            System.Globalization.CultureInfo culture)
        {
            string val = string.Format("{0}", value);

            int numValue = 0;
            if (!Int32.TryParse(val, out numValue))
            {
                val = val.ToUpper();

                switch (val)
                {
                    case "ONE":
                        numValue = 1;
                        break;
                    case "TWO":
                        numValue = 2;
                        break;
                    case "THREE":
                        numValue = 3;
                        break;
                    case "FOUR":
                        numValue = 4;
                        break;
                    case "FIVE":
                        numValue = 5;
                        break;
                    case "SIX":
                        numValue = 6;
                        break;
                    case "SEVEN":
                        numValue = 7;
                        break;
                    case "EIGHT":
                        numValue = 8;
                        break;
                    case "NINE":
                        numValue = 9;
                        break;
                    case "TEN":
                        numValue = 10;
                        break;                    
                }
            }

            return numValue == 0? value : numValue;
        }
    }
}
